//
//  CZRefreshControl.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/30.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit

class CZRefreshControl: UIRefreshControl {

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///属性
    //refreshView的y的偏移值
    private let refreshOffsetY: CGFloat = -60
    
    //箭头方向的标志
    var isUp = false
    //覆盖父类的frame属性
   override var frame: CGRect {
    
    didSet{
        if frame.origin.y > 0{
        
            return
        }
        
        //判断动画是否正在刷新
        if refreshing {
        
//            print("菊花正在刷新")
            refreshView.startLoading()
        }
        if frame.origin.y < refreshOffsetY && !isUp{
        
//            print("箭头向下，向上转")
            refreshView.rotationTipIcon(isUp)
            isUp = !isUp
        }else if frame.origin.y > refreshOffsetY && isUp{
        
//            print("箭头向上，向下转")
            refreshView.rotationTipIcon(isUp)
            isUp = !isUp
        }
//        print("frame:\(frame)")
    }

    }
    override init() {
        super.init()
        
        prepareUI()
    }
    //重写UIRefreshControl的endRefreshing方法
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.endLoad()
        
    }
    
    ///MARK: - 准备UI
    func prepareUI() {
        //添加控件
        //view从xib里面加载出来的时候，已经有大小了，大小是在xib里面设置的大小
        addSubview(refreshView)
        //添加约束
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
    }

    ///MARK: - 懒加载
    private lazy var refreshView: CZRefreshView = CZRefreshView.refreshView()
}

//自定义的刷新控件的view
class CZRefreshView:UIView {

    //遮罩view
    @IBOutlet weak var tipView: UIView!
    //菊花
    @IBOutlet weak var juhua: UIImageView!
    
    //箭头
    @IBOutlet weak var tipIcon:UIImageView!
    //从xib把view加载出来
    class func refreshView() ->CZRefreshView {
    
        //xib的路径
        let view = NSBundle.mainBundle().loadNibNamed("RefreshView", owner: nil, options: nil).last as! CZRefreshView
        return view
    }
    
    ///箭头旋转
    func rotationTipIcon(isUp: Bool){
    
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.tipIcon.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.0001)) : CGAffineTransformIdentity
        }
    
    }
    
    ///MARK: -菊花旋转
    func startLoading() {
    
        let animatKey = "animatKey"
        //判断菊花是否正在刷新,如果正在执行就直接返回
       if (juhua.layer.animationForKey(animatKey) != nil){
        
//            print("菊花正在刷新")
            return
        }
        //隐藏遮罩view
        tipView.hidden = true
        
        //创建核心动画
        let animat = CABasicAnimation(keyPath: "transform.rotation")
        //动画时间
        animat.duration = 0.25
        animat.toValue = M_PI * 2
        
        //动画次数
        animat.repeatCount = MAXFLOAT
        
        //不移除动画
        animat.removedOnCompletion = false
        
        //开始菊花动画(核心动画)
        juhua.layer.addAnimation(animat, forKey: animatKey)
    }
    
    ///MARK: - 结束加载
    ///结束加载
    func endLoad() {
    
        //显示箭头所在的view
        tipView.hidden = false
        //关闭菊花旋转(移除菊花的动画)
        juhua.layer.removeAllAnimations()
    }
}





